﻿using System.Collections.Generic;
using MRGSP.SurveyMaster.Core;
using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Core.Repository;
using MRGSP.SurveyMaster.Core.Service;

namespace MRGSP.SurveyMaster.Service.Services
{
    public class QuestionService : IQuestionService
    {
        private readonly IQuestionRepository repo;

        public QuestionService(IQuestionRepository repo)
        {
            this.repo = repo;
        }

        public long Create(long? parentId, int type, string text, long surveyId, bool isMandatory)
        {
            return repo.Insert(parentId, text, type, surveyId, isMandatory);
        }

        public bool Update(long id, string text, int typeId, bool isMandatory)
        {
            return repo.Update(id, text, typeId, isMandatory).Equals(1);
        }

        public bool Update(long id, string text, bool isMandatory)
        {
            var q = repo.Get(id);
            return repo.Update(id, text, (int) q.Type, isMandatory).Equals(1);
        }

        public IEnumerable<Question> GetBySurveyId(long surveyId)
        {
            return repo.GetBySurveyId(surveyId);
        }

        public bool Delete(long id)
        {
            return repo.Delete(id).Equals(1);
        }

        public Question Get(long id)
        {
            return repo.Get(id);
        }

        public IEnumerable<Question> GetByQuestionId(long id)
        {
            return repo.GetByQuestionId(id);
        }

        public void AddColumn(long gridId, string text, bool columnIsMandatory)
        {
            repo.InsertColumnQuestions(gridId, text, (int) ControlType.GridColumn, columnIsMandatory);
        }

        public void AddCellElement(long columnId, string text, int typeId)
        {
            repo.InsertCellQuestions(columnId, text, typeId);
        }

        public long GetGrandGrandPa(long id)
        {
            return repo.GetParentId(repo.GetParentId(id));
        }

        public long GetGrandPaId(long id)
        {
            return repo.GetParentId(repo.GetParentId(id));
        }

        public bool DeleteColumn(long columnId)
        {
            return repo.DeleteByColumnId(columnId) > 0;
        }

        public long GetGrandPaIdByColumnId(long columnId)
        {
            return repo.GetGrandPaIdByColumnId(columnId);
        }

        public void AddRow(long parentId, int typeId, string text, long surveyId)
        {
            repo.InsertRow(parentId, typeId, text, surveyId);
        }
    }
}